package com.google.code.swingonfire.border;

import java.util.LinkedHashMap;

public class ShadowCircleCache extends LinkedHashMap<ShadowCircleCacheKey, ShadowCircleCacheEntry>
{

    private static final long serialVersionUID = -349747859622931302L;

    private static ShadowCircleCache instance = new ShadowCircleCache(8);

    public static ShadowCircleCache getInstance()
    {
        return instance;
    }

    public static void setCapacity(int capacity)
    {
        ShadowCircleCache.instance = new ShadowCircleCache(capacity);
    }

    private final int capacity;

    private ShadowCircleCache(int capacity)
    {
        super(capacity, 0.75f, true);

        this.capacity = capacity;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ShadowCircleCacheEntry get(Object key)
    {
        ShadowCircleCacheEntry result = super.get(key);

        if (result == null)
        {
            ShadowCircleCacheKey shadowCircleCacheKey = (ShadowCircleCacheKey) key;

            result = new ShadowCircleCacheEntry(shadowCircleCacheKey);
            put(shadowCircleCacheKey, result);
        }

        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry<ShadowCircleCacheKey, ShadowCircleCacheEntry> eldest)
    {
        return size() > capacity;
    }

}
